<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>tree-table</title>
    <link rel="stylesheet" href="../res/layui/css/layui.css">
    <link rel="stylesheet" href="../res/layui/css/common.js"/>
</head>
<body>
<div class="demoTable">
    搜索ID：
    <div class="layui-inline">
        <input id="edt-search" type="text" placeholder="输入关键字" style="width: 120px;height: 30px"/>&nbsp;&nbsp;
        <button class="layui-btn" id="btn-search">&nbsp;&nbsp;搜索&nbsp;&nbsp;</button>
    </div>
</div>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-group">
        <button type="button" class="layui-btn" lay-event="zk">全部展开</button>
        <button type="button" class="layui-btn" lay-event="zd">全部折叠</button>
        <button type="button" class="layui-btn" lay-event="sx">刷新表格</button>
        <button type="button" class="layui-btn layui-btn-warm" lay-event="zj">增加</button>
    </div>
</script>
<table id="table1" class="layui-table" lay-filter="table1"></table>
<!-- 操作列 -->
<script type="text/html" id="oper-col">
    <button type="button" class="layui-btn layui-btn-sm" lay-event="edit"><i class="layui-icon"></i></button>
    <button type="button" class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del"><i class="layui-icon"></i></button>
</script>

<script src="../res/layui/layui.js"></script>
<table id="demo" lay-filter="test" ></table>
<script>
    layui.config({
        base: '../res/'
    }).extend({
        treetable: 'treetable-lay/treetable'
    }).use(['table', 'form', 'element', 'treetable'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        var form = layui.form;
        var element = layui.element;
        var treetable = layui.treetable;

        // 渲染表格
        var renderTable = function () {
            layer.load(2);
            treetable.render({
                treeColIndex: 2,
                treeSpid: 0,
                treeIdName: 'itemId',
                treePidName: 'parentId',
                elem: '#table1',
                url: '../AtsBaseItem/findAll',
                page: false,
                toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
                cols: [[
                    {field: 'itemExp', title: '指标名称', width:150},
                    {field: 'itemExp', title: '指标层级', width:150},
                    {field: 'itemUnit', title: '指标单位', width:150},
                    {field: 'creater', title: '创建人', width:150},
                    {field: 'creatTime', title: '创建时间', width:150},
                    {field: 'isva', title: '备注', width:150},
                    {templet: '#oper-col', title: '操作', width:150}
                ]],
                done: function () {
                    layer.closeAll('loading');
                }
            });
        };

        $('#btn-expand').click(function () {
            treetable.expandAll('#table1');
        });

        $('#btn-fold').click(function () {
            treetable.foldAll('#table1');
        });

        $('#btn-refresh').click(function () {
            renderTable();
        });
        $('#btn-search').click(function () {
            var keyword = $('#edt-search').val();
            var searchCount = 0;
            $('#table1').next('.treeTable').find('.layui-table-body tbody tr td').each(function () {
                $(this).css('background-color', 'transparent');
                var text = $(this).text();
                if (keyword != '' && text.indexOf(keyword) >= 0) {
                    $(this).css('background-color', 'rgba(250,230,160,0.5)');
                    if (searchCount == 0) {
                        treetable.expandAll('#table1');
                        $('html,body').stop(true);
                        $('html,body').animate({scrollTop: $(this).offset().top - 150}, 500);
                    }
                    searchCount++;
                }
            });
            if (keyword == '') {
                layer.msg("请输入搜索内容", {icon: 5});
            } else if (searchCount == 0) {
                layer.msg("没有匹配结果", {icon: 5});
            }
        });
        renderTable();
        table.on('toolbar(table1)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id); //获取选中行状态
            switch(obj.event){
                case 'sx':
                    renderTable();
                    break;
                case 'zk':
                    treetable.expandAll('#table1');
                    break;
                case 'zd':
                    treetable.foldAll('#table1');
                    break;
                case 'zj':
                    layer.open({
                        type: 2,
                        title:'指标管理新增',
                        shadeClose:true,
                        shade:0.8,
                        area:['50%','50%'],
                        content: ['AtsBaseItemUpdate.html'], //数组第二项即吸附元素选择器或者DOM
                        end:function(){
                            //刷新表格
                            renderTable();
                        }
                    });
                    break;

            };
        });
        table.on('tool(table1)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'del') {
                layer.msg('删除' + data.id);
                var url = "../AtsBaseItem/ljsc";
                var data1 = {id:data.id};
                $.post(url,data1,function(obj){
                    layer.msg(obj.msg);
                    renderTable();
                },"json");
            } else if (layEvent === 'edit') {
                layer.open({
                    type: 2,
                    title:'指标管理修改',
                    shadeClose:true,
                    shade:0.8,
                    area:['50%','50%'],
                    content: ['AtsBaseItemUpdate.html?id='+data.id], //数组第二项即吸附元素选择器或者DOM
                    end:function(){
                        //刷新表格
                        renderTable();
                    }
                });
            }
        });
    });
</script>
</body>
</html>